/*     */ package com.asiainfo.dacp.stat.olapcheck;
/*     */ 
/*     */ import com.asiainfo.dacp.jdbc.JdbcTemplate;
/*     */ import com.asiainfo.dacp.util.JsonHelper;
/*     */ import com.asiainfo.dacp.util.TaskDate;
/*     */ import java.io.PrintStream;
/*     */ import java.math.BigDecimal;
/*     */ import java.util.ArrayList;
/*     */ import java.util.Iterator;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import org.slf4j.Logger;
/*     */ import org.slf4j.LoggerFactory;
/*     */ 
/*     */ 
/*     */ public class OlapInfo
/*     */ {
/*  18 */   public String olapCode = null;
/*  19 */   public String taskid = "";
/*  20 */   String ptaskid = ""; String pmtaskid = ""; String pytaskid = "";
/*  21 */   public String comparetime = null;
/*  22 */   String tabname = null;
/*  23 */   String timefield = "optime";
/*  24 */   String timefieldType = "string";
/*  25 */   String cycletype = "D";
/*  26 */   String zbfield = "";
/*  27 */   private String sumSql = "select sum(case when {timefield}='{taskid}' then {zbfield} else 0 end) val,sum(case when {timefield}='{ptaskid}' then value({zbfield},0) else 0 end) pval,sum(case when {timefield}='{ptaskid}' then 1 else 0 end) r_sumcube from {tabname} where {timefield} in('{taskid}','{ptaskid}')";
/*  28 */   private String dimSql = "select {dimid},sum(case when {timefield}='{taskid}' then {zbfield} else 0 end) val,sum(case when {timefield}='{ptaskid}' then value({zbfield},0) else 0 end) pval from {tabname} where {timefield} in('{taskid}','{ptaskid}') group by {dimid}";
/*  29 */   private String nowSelectSql = "";
/*  30 */   private String cfgDBName = "";
/*  31 */   private String dataDBName = "";
/*  32 */   public List<DimInfo> dimList = new ArrayList();
/*     */   public double val;
/*     */   public double pval;
/*     */   public double addval;
/*     */   public double drate;
/*     */   public double mrate;
/*     */   public int sumcube;
/*     */   public int addcube;
/*  40 */   public int losecube; private List<Map> outdimList; private static Logger logger = LoggerFactory.getLogger(OlapInfo.class);
/*     */   
/*     */ 
/*     */ 
/*     */   public OlapInfo(String olapCode)
/*     */   {
/*  46 */     this.olapCode = olapCode;
/*     */     
/*  48 */     JdbcTemplate jdbcTemplate = new JdbcTemplate(this.cfgDBName);
/*  49 */     List data = jdbcTemplate.queryForList("select MODELCODE, MODELNAME,TABNAME from BUSI_MODELLIST where modelcode='" + olapCode + "'");
/*  50 */     if (data.size() == 0) {
/*  51 */       System.out.println("error");
/*     */     }
/*  53 */     Map map = (Map)data.get(0);
/*  54 */     this.tabname = ((String)map.get("tabname"));
/*     */     
/*     */ 
/*  57 */     List olapAttrList = jdbcTemplate.queryForList("select  ATTRCODE, PARENTCODE, ATTNAME, DATATYPE, ATTRTYPE,FKDIM_ID from BUSI_MODELATTR  where modelcode='" + olapCode + "'");
/*  58 */     if (olapAttrList.size() == 0) {
/*  59 */       System.out.println("error");
/*  60 */       return;
/*     */     }
/*  62 */     for (int i = 0; i < olapAttrList.size(); i++) {
/*  63 */       Map attrMap = (Map)olapAttrList.get(i);
/*  64 */       String attrType = (String)attrMap.get("attrtype");
/*  65 */       if (attrType.indexOf("维度-时间") >= 0) {
/*  66 */         this.timefield = ((String)attrMap.get("attrcode"));
/*  67 */         this.timefieldType = ((String)attrMap.get("datatype"));
/*     */       }
/*  69 */       else if (attrType.indexOf("指标") >= 0) {
/*  70 */         this.zbfield = ((String)attrMap.get("attrcode"));
/*     */       }
/*  72 */       else if (attrType.indexOf("维度") >= 0) {
/*  73 */         DimInfo dimInfo = new DimInfo((String)attrMap.get("attrcode"), this);
/*  74 */         this.dimList.add(dimInfo);
/*     */       }
/*     */     }
/*  77 */     System.out.println(this.tabname + ",timefield:" + this.timefield + ",zbfield=" + this.zbfield + ",dim num=" + this.dimList.size());
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   public Integer loadOlapData(String optime, String comparetime, String where)
/*     */   {
/*  85 */     this.taskid = optime;
/*  86 */     this.comparetime = comparetime;
/*  87 */     initTaskInfo();
/*  88 */     if ((where == null) || (where.equals(""))) {
/*  89 */       JdbcTemplate cfgjdbcTemplate = new JdbcTemplate(this.cfgDBName);
/*  90 */       cfgjdbcTemplate.execute("delete from MINDER_OLAPDIM_CHKDETAIL where OLAPCODE='" + this.olapCode + "' and OPTIME='" + this.taskid + "' and COMPARETIME='" + this.ptaskid + "'");
/*  91 */       cfgjdbcTemplate.execute("delete from MINDER_OLAPDIM_CHKINFO where OLAPCODE='" + this.olapCode + "' and OPTIME='" + this.taskid + "' and COMPARETIME='" + this.ptaskid + "'");
/*     */     }
/*  93 */     String sql = delaSqlText(this.sumSql);
/*  94 */     System.out.println("sum sql:" + sql);
/*  95 */     JdbcTemplate jdbcTemplate = new JdbcTemplate(this.dataDBName);
/*  96 */     List sumData = jdbcTemplate.queryForList(sql);
/*  97 */     Map map = (Map)sumData.get(0);
/*  98 */     this.val = getDouble(map.get("val"));
/*  99 */     this.pval = getDouble(map.get("pval"));
/* 100 */     this.addval = MathUtil.subtract(Double.valueOf(this.val), Double.valueOf(this.pval)).doubleValue();
/* 101 */     this.sumcube = ((Integer)map.get("r_sumcube")).intValue();
/* 102 */     this.drate = MathUtil.divide(MathUtil.subtract(Double.valueOf(this.val), Double.valueOf(this.pval)), Double.valueOf(this.val)).doubleValue();
/* 103 */     JsonHelper jsonHelper = JsonHelper.getInstance();
/* 104 */     System.out.println(jsonHelper.write(this));
/* 105 */     proceDimData(where);
/* 106 */     if ((where == null) || (where.equals(""))) {
/* 107 */       saveSumInfoToDb();
/*     */     }
/* 109 */     return Integer.valueOf(0);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public String loadOlapData(String optime, String comparetime, String where, String outdim)
/*     */   {
/* 116 */     dealParameter(where, outdim);
/* 117 */     loadOlapData(optime, comparetime, where);
/*     */     
/* 119 */     List<DimInfo> list = new ArrayList();
/* 120 */     for (Iterator localIterator1 = this.outdimList.iterator(); localIterator1.hasNext();) { outDimObj = (Map)localIterator1.next();
/* 121 */       for (DimInfo diminfo : this.dimList)
/* 122 */         if (diminfo.dimid.equals(outDimObj.get("dimid"))) {
/* 123 */           list.add(diminfo);
/* 124 */           break;
/*     */         }
/*     */     }
/*     */     Map outDimObj;
/* 128 */     this.dimList = list;
/* 129 */     return JsonHelper.getInstance().write(this);
/*     */   }
/*     */   
/*     */   public void dealParameter(String where, String outdim) {
/* 133 */     if ((where != null) && (!where.equals(""))) {
/* 134 */       List<Map> whereList = (List)JsonHelper.getInstance().read(where);
/* 135 */       String whereSql = "1=1";
/* 136 */       for (Map map : whereList) {
/* 137 */         String whereDimid = (String)map.get("dimid");
/* 138 */         List<Map> member = (List)map.get("member");
/* 139 */         String brandSql = whereDimid + " in ('";
/* 140 */         for (Map row : member) {
/* 141 */           brandSql = brandSql + row.get(whereDimid) + "','";
/*     */         }
/* 143 */         whereSql = whereSql + " and " + brandSql.substring(0, brandSql.length() - 2) + ")";
/*     */       }
/* 145 */       this.tabname = ("(select * from " + this.tabname + " where " + whereSql + ")");
/* 146 */       if ((outdim != null) && (!outdim.equals(""))) {
/* 147 */         this.outdimList = ((List)JsonHelper.getInstance().read(outdim));
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   public void proceDimData(String where) {
/* 153 */     for (int i = 0; i < this.dimList.size(); i++) {
/* 154 */       DimInfo diminfo = (DimInfo)this.dimList.get(i);
/* 155 */       if (this.outdimList != null) {
/* 156 */         for (Map map : this.outdimList)
/*     */         {
/* 158 */           if (diminfo.dimid.equals(map.get("dimid"))) {
/* 159 */             String dimdatasql = delaSqlText(this.dimSql, diminfo.dimid);
/* 160 */             diminfo.proceDim(dimdatasql, this.dataDBName, where);
/* 161 */             break;
/*     */           }
/*     */         }
/*     */       } else {
/* 165 */         String dimdatasql = delaSqlText(this.dimSql, diminfo.dimid);
/* 166 */         diminfo.proceDim(dimdatasql, this.dataDBName, where);
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   private void initTaskInfo() {
/* 172 */     if (this.taskid.length() == 6) {
/* 173 */       this.cycletype = "M";
/* 174 */       this.ptaskid = TaskDate.getTaskID(this.taskid, "m", Integer.valueOf(-1));
/* 175 */       this.pmtaskid = this.ptaskid;
/* 176 */       this.pytaskid = TaskDate.getTaskID(this.taskid, "y", Integer.valueOf(-1));
/*     */     } else {
/* 178 */       this.cycletype = "D";
/* 179 */       this.ptaskid = TaskDate.getTaskID(this.taskid, "d", Integer.valueOf(-1));
/* 180 */       this.pmtaskid = TaskDate.getTaskID(this.taskid, "m", Integer.valueOf(-1));
/* 181 */       this.pytaskid = TaskDate.getTaskID(this.taskid, "y", Integer.valueOf(-1));
/*     */     }
/*     */     
/* 184 */     if ((this.comparetime != null) && (!this.comparetime.equals(""))) {
/* 185 */       this.ptaskid = this.comparetime;
/*     */     }
/*     */   }
/*     */   
/*     */   protected static double getDouble(Object val)
/*     */   {
/*     */     try
/*     */     {
/* 193 */       if (val == null)
/* 194 */         return 0.0D;
/* 195 */       if ((val instanceof BigDecimal))
/* 196 */         return ((BigDecimal)val).doubleValue();
/* 197 */       if ((val instanceof Integer))
/* 198 */         return ((Integer)val).doubleValue();
/* 199 */       if ((val instanceof Float))
/* 200 */         return ((Float)val).doubleValue();
/* 201 */       if ((val instanceof String)) {
/* 202 */         String valStr = (String)val;
/* 203 */         if (valStr.indexOf(".") < 0)
/* 204 */           return Integer.parseInt(valStr);
/* 205 */         return Double.parseDouble(valStr); }
/* 206 */       if ((val instanceof Long)) {
/* 207 */         return Double.valueOf(val.toString()).doubleValue();
/*     */       }
/*     */       
/* 210 */       return ((Double)val).doubleValue();
/*     */ 
/*     */     }
/*     */     catch (Exception e)
/*     */     {
/* 215 */       e.printStackTrace(); }
/* 216 */     return -999.0D;
/*     */   }
/*     */   
/*     */   private String delaSqlText(String insql) {
/* 220 */     String resultsql = insql;
/* 221 */     if ("int".equals(this.timefieldType)) {
/* 222 */       for (int i = 0; i < 10; i++) {
/* 223 */         resultsql = resultsql.replace("'{taskid}'", this.taskid);
/* 224 */         resultsql = resultsql.replace("'{ptaskid}'", this.ptaskid);
/* 225 */         resultsql = resultsql.replace("{tabname}", this.tabname);
/* 226 */         resultsql = resultsql.replace("{timefield}", this.timefield);
/* 227 */         resultsql = resultsql.replace("{zbfield}", this.zbfield);
/*     */       }
/*     */       
/*     */     } else {
/* 231 */       for (int i = 0; i < 10; i++) {
/* 232 */         resultsql = resultsql.replace("{taskid}", this.taskid);
/* 233 */         resultsql = resultsql.replace("{ptaskid}", this.ptaskid);
/* 234 */         resultsql = resultsql.replace("{tabname}", this.tabname);
/* 235 */         resultsql = resultsql.replace("{timefield}", this.timefield);
/* 236 */         resultsql = resultsql.replace("{zbfield}", this.zbfield);
/*     */       }
/*     */     }
/* 239 */     return resultsql;
/*     */   }
/*     */   
/* 242 */   private String delaSqlText(String insql, String dimid) { String resultsql = delaSqlText(insql);
/* 243 */     for (int i = 0; i < 10; i++) {
/* 244 */       resultsql = resultsql.replace("{dimid}", dimid);
/*     */     }
/* 246 */     return resultsql;
/*     */   }
/*     */   
/*     */   public void saveSumInfoToDb() {
/* 250 */     JdbcTemplate jdbcTemplate = new JdbcTemplate("");
/* 251 */     String sql = "insert into MINDER_OLAPDIM_CHKDETAIL(OLAPCODE,OPTIME,DIMCODE,ITEMCODE,VAL,PVAL,DRATE,ADDVAL,ITEMNAME,COMPARETIME) values(?,?,?,?,?,?,?,?,?,?)";
/*     */     
/*     */     try
/*     */     {
/* 255 */       jdbcTemplate.update(sql, new Object[] { this.olapCode, this.taskid, "sum", "sum", 
/*     */       
/*     */ 
/* 258 */         Double.valueOf(this.val), Double.valueOf(this.pval), Double.valueOf(this.drate), Double.valueOf(this.addval), "总体", this.ptaskid });
/*     */ 
/*     */     }
/*     */     catch (Exception sqlex)
/*     */     {
/* 263 */       logger.info("保存总体信息错误:" + sqlex.toString() + "sql:" + sql);
/* 264 */       sqlex.printStackTrace();
/*     */     }
/*     */   }
/*     */ }


/* Location:              C:\Users\my\.m2\repository\com\asiainfo\dacp\dacp-dataos\1.0.0\dacp-dataos-1.0.0.jar!\com\asiainfo\dacp\stat\olapcheck\OlapInfo.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */